﻿using log4net;
using Newtonsoft.Json.Linq;
using Npgsql;
using PlaceOrder_Core.Helper;
using PlaceOrder_Core.PlaceOrderModels;
using PlaceOrder_Core.Services.Interfaces;
using PlaceOrder_Shared.Models.Common;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace PlaceOrder_Core.Services.Instances
{
    public class Data:IData
    {
        private readonly IDatabase _database;
        private readonly ILog _log;
        private readonly IDataHelper _dataHelper;
        readonly NpgsqlConnection connection;
        public static string LoginIP = "";
        public Data( IDatabase database_,ILog log,IDataHelper dataHelper)
        {
            _database = database_;
            _log = log;
            _dataHelper = dataHelper;
        }



        #region User相关
        public int GetLilyTradeLimit(int UserID)
        {
            int MaxVolume = 0;
            try
            {
                using (var db = _database.GetReaderContext())
                {
                    var role = db.SysRoles.FirstOrDefault(m => m.DataScope == "3");
                    if (role != null)
                    {
                        MaxVolume = (int)role.RoleId;
                    }
                    
                }
            }
            catch (Exception ex)
            {
                _log.Error(ex.Message+"DataHelper"+"GetLilyTradeLimit"+ex.StackTrace);
            }
            return MaxVolume;
        }

        public int GetUserID(string UserName, out string NickName)
        {
            int userid = 0;
            try
            {
                using (var db = _database.GetReaderContext())
                {
                    if (!db.IsDbConnectionOpen())
                    {
                        NickName = null;
                        return userid;
                    }
                    var user = db.SysUsers
                                .Where(o => o.UserName == UserName)
                                .Select(o => new { o.UserId, o.NickName })
                                .FirstOrDefault();
          
                    if (user != null)
                    {
                        userid = (int)user.UserId;
                        NickName = user.NickName;
                    }
                    else
                    {
                        NickName = "Unknown";  // 当未找到用户时，为 nickName 赋一个默认值
                    }

                }
            }
            catch (Exception ex)
            {
                NickName = "null";
                AppLog.WriteError(ex.Message + "Data" + "GetUserID" + ex.StackTrace);
            }
            
            return userid;
        }




        public static string Token = "";




        public void GetLocalIP()
        {
            var host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (var ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    LoginIP = ip.ToString();
                }
            }
        }
        #endregion


        #region 客户端相关接口实现

        #endregion

        #region 服务端相关接口实现

        #endregion
    }
}
